/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-05-09
 * Time: 20:19
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root;

    /**
     * 查找
     * 空间复杂度：O(logN)
     * @param val
     * @param root
     * @return
     */
    public TreeNode search1(int val, TreeNode root) {
        if(root == null) {
            return null;
        }
        if (root.val > val) {
            search1(val,root.left);
        } else if(root.val < val) {
            search1(val, root.right);
        } else {
            return root;
        }
        return null;
    }
    public TreeNode search(int val) {
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            } else if(cur.val > val) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }
    public boolean insert(int val) {
        TreeNode node = new TreeNode(val);
        if (root == null) {
            root = node;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            } else if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            } else {
                return false;
            }
        }
        if(parent.val < val) {
            parent.right = node;
        } else {
            parent.left = node;
        }
        return true;
    }
    public void removeNode(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else {
                remove(cur, parent);
            }
        }
    }
    private void remove(TreeNode cur, TreeNode parent) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {
            TreeNode t = cur.right;
            TreeNode tp = cur;
            while(t.left != null) {
                tp = t;
                t = t.left;
            }
            cur.val = t.val;
            if (t == tp.left) {
                tp.left = t.right;
            } else {
                tp.right = t.right;
            }

        }
    }
}
